/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;
import controller.Conta;
import controller.ContaCorrente;
import controller.ContaInvestimento;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
import model.ContaDAO;
/**
 *
 * @author Carol
 */
public class frm_saldo extends javax.swing.JFrame {

    /**
     * Creates new form frm_saldo
     */
    private int nConta = 0;
    private int tipoConta;
    private Conta conta;
    DecimalFormat df = new DecimalFormat("0.00");
    
    public frm_saldo() {
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);   
        this.setLocationRelativeTo(null);
        this.setTitle("Banco Internacional de TADS - Informações da Conta");
            
    } 
    
    public frm_saldo(int nConta){
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("Banco Internacional de TADS - Informações da Conta");
        this.nConta = nConta; 
        lblNumConta.setText(Integer.toString(nConta));  

            ContaDAO contadao;
        try {
            contadao = new ContaDAO();
            String tipo = contadao.buscarTipoConta(nConta);
            if("CONTA CORRENTE".equals(tipo)){
                ContaCorrente conta = (ContaCorrente)contadao.buscarConta(nConta);
                lblValorLimite.setText(df.format(conta.getLimite()));
                lblValorSaldo.setText(df.format(conta.getSaldo()));                
                lblValorTotal.setText(df.format(conta.getSaldo() + conta.getLimite()));
                this.conta = (Conta) conta;
                this.tipoConta = 1;
                //ContaCorrente cc = new ContaCorrente(nConta, conta.getDepini(), conta.getSaldo(), conta.getLimite());              
            }else{
                ContaInvestimento conta = (ContaInvestimento) contadao.buscarConta(nConta);
                lblValorLimite.setText("-");
                lblValorSaldo.setText(df.format(conta.getSaldo()));
                lblValorTotal.setText(lblValorSaldo.getText());
                this.conta = (Conta) conta;
                this.tipoConta = 2;
            }
        } catch (Exception ex) {
            Logger.getLogger(frm_saldo.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        lblContaNo = new javax.swing.JLabel();
        lblNumConta = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblSaldo = new javax.swing.JLabel();
        lblValorSaldo = new javax.swing.JLabel();
        cmdSaque = new javax.swing.JButton();
        cmdDeposito = new javax.swing.JButton();
        cmdRemunera = new javax.swing.JButton();
        lblLimite = new javax.swing.JLabel();
        lblValorLimite = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblValorTotal = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblContaNo.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        lblContaNo.setText("Conta n°");

        lblNumConta.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        lblNumConta.setText("X");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(lblContaNo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNumConta, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblContaNo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblNumConta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        lblSaldo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblSaldo.setText("Saldo Atual:");

        lblValorSaldo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblValorSaldo.setText("XX,XX");

        cmdSaque.setText("Saque");
        cmdSaque.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cmdSaqueMouseReleased(evt);
            }
        });

        cmdDeposito.setText("Depósito");
        cmdDeposito.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cmdDepositoMouseReleased(evt);
            }
        });

        cmdRemunera.setText("Remunera");
        cmdRemunera.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cmdRemuneraMouseReleased(evt);
            }
        });

        lblLimite.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblLimite.setText("Limite:");

        lblValorLimite.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblValorLimite.setText("XX,XX");

        lblTotal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblTotal.setText("Total:");

        lblValorTotal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblValorTotal.setText("XX,XX");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdSaque, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addComponent(cmdDeposito, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(cmdRemunera, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jSeparator2)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(lblTotal)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblValorTotal))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblSaldo)
                                .addComponent(lblLimite))
                            .addGap(87, 87, 87)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lblValorLimite)
                                .addComponent(lblValorSaldo)))))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblValorSaldo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLimite)
                    .addComponent(lblValorLimite))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblValorTotal)
                    .addComponent(lblTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdSaque, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdDeposito, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdRemunera, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdSaqueMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdSaqueMouseReleased
        // TODO add your handling code here:
        frm_saque telaSaque = new frm_saque(conta, tipoConta);
        telaSaque.show();      
        this.dispose();
    }//GEN-LAST:event_cmdSaqueMouseReleased

    private void cmdDepositoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdDepositoMouseReleased
        // TODO add your handling code here:
        frm_deposito telaDeposito = new frm_deposito(conta, tipoConta);
        telaDeposito.show();
        this.dispose();
    }//GEN-LAST:event_cmdDepositoMouseReleased

    private void cmdRemuneraMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdRemuneraMouseReleased
        // TODO add your handling code here:
        //Tela de confirmação para remuneração
        int resposta;
        resposta = JOptionPane.showConfirmDialog(null,"Confirma a remuneração da conta?");
        //0 -> JOptionPane.YES_OPTION
        //1 -> JOptionPane.NO_OPTION
        //2 -> JOptionPane.CANCEL_OPTION
        if(resposta == 0){
            //faz a remuneração da conta
            try {
                ContaDAO contadao = new ContaDAO();
                String tipo = contadao.buscarTipoConta(nConta);
                if("CONTA CORRENTE".equals(tipo)){
                    ContaCorrente cc = (ContaCorrente) contadao.buscarConta(nConta);
                    cc.remunera();
                    lblValorSaldo.setText(df.format(cc.getSaldo())); 
                    lblValorTotal.setText(df.format(cc.getSaldo() + cc.getLimite()));
                }else{
                    ContaInvestimento ci = (ContaInvestimento) contadao.buscarConta(nConta);
                    ci.remunera();
                    lblValorSaldo.setText(df.format(ci.getSaldo())); 
                    lblValorTotal.setText(df.format(ci.getSaldo()));
                }
                JOptionPane.showMessageDialog(null,"Remuneração realizada com sucesso!");
            } catch (Exception ex) {
                Logger.getLogger(frm_saldo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            JOptionPane.showMessageDialog(null,"Remuneração cancelada!");
        }        
    }//GEN-LAST:event_cmdRemuneraMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frm_saldo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frm_saldo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frm_saldo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frm_saldo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frm_saldo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdDeposito;
    private javax.swing.JButton cmdRemunera;
    private javax.swing.JButton cmdSaque;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblContaNo;
    private javax.swing.JLabel lblLimite;
    private javax.swing.JLabel lblNumConta;
    private javax.swing.JLabel lblSaldo;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblValorLimite;
    private javax.swing.JLabel lblValorSaldo;
    private javax.swing.JLabel lblValorTotal;
    // End of variables declaration//GEN-END:variables
}
